package user

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"strings"
)

// 购买成功通知
func Buychenggong(openid, value, amount3, time6 string) {
	token := Accesstoken()
	d := map[string]interface{}{
		"touser":      openid,
		"template_id": "uTUSk1z9N5aLEilOsF1EqrS58nbdJg3k3d1KYu8eUb0",
		"url":         "http://h.suishilian.com/businesspush?pid=?pid=10590",
		"data": map[string]interface{}{
			"thing1": map[string]interface{}{ //产品名称
				"value": value,
			},
			"time4": map[string]interface{}{ //合计数量
				"value": amount3,
			},
			"character_string10": map[string]interface{}{ //制单人
				"value": time6,
			},
			"thing7": map[string]interface{}{ //制单人
				"value": "您发布的商机已有人购买！",
			},
		},
	}
	dJson, _ := json.Marshal(d)
	sJson := string(dJson)
	HttpPost("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+token, sJson, "", d)
	//fmt.Println(resp)
}

// 余额到账通知
func Yuertongzhi(openid, amount3 string) {
	token := Accesstoken()
	d := map[string]interface{}{
		"touser":      openid,
		"template_id": "uTUSk1z9N5aLEilOsF1EqrS58nbdJg3k3d1KYu8eUb0",
		"url":         "http://h.suishilian.com/user",
		"data": map[string]interface{}{
			"thing25": map[string]interface{}{ //产品名称
				"value": "您有一份新的收益待提现！",
			},
			"amount1": map[string]interface{}{ //合计数量
				"value": amount3,
			},
		},
	}
	dJson, _ := json.Marshal(d)
	sJson := string(dJson)
	HttpPost("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+token, sJson, "", d)
	//fmt.Println(resp)
}
func HttpPost(url, params string, contentType string, retMap interface{}) error {
	if contentType == "" {
		contentType = "application/json"
		// "application/x-www-form-urlencoded"
	}
	//fmt.Println(params)
	resp, err := http.Post(url,
		contentType,
		strings.NewReader(params))
	if err != nil {
		return err
	}

	defer func() {
		_ = resp.Body.Close()
	}()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return err
	}
	fmt.Println(string(body))
	err = json.Unmarshal(body, retMap)
	if err != nil {
		return err
	}
	return nil
}

type ResponseAccess struct {
	AccessToken string `json:"access_token"`
	ExpiresIn   int64  `json:"expires_in"`
}
